package com.yycx.module.bus.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.module.bus.client.entity.hy.BusInvoiceunitQuota;
import com.yycx.module.bus.provider.mapper.hy.BusInvoiceunitQuotaMapper;
import com.yycx.module.bus.provider.service.BusInvoiceunitQuotaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 开票单位月额度明细接口实现类
 *
 * @author flyme
 * @date 2023-09-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusInvoiceunitQuotaServiceImpl extends BaseServiceImpl<BusInvoiceunitQuotaMapper, BusInvoiceunitQuota> implements BusInvoiceunitQuotaService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusInvoiceunitQuota biq, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusInvoiceunitQuota> cq, BusInvoiceunitQuota biq, EntityMap requestMap) {
        cq.eq("invoiceUnitId");
        cq.eq("quotaYear");
        cq.eq("quotaMonth");
        cq.orderByDesc("biq.createTime");
        return ResultBody.ok();
    }

    @Override
    public List<BusInvoiceunitQuota> getYearQuotaBy(Long invoiceUnitId, int quotaYear, String quotaMonth) {
        LambdaQueryWrapper<BusInvoiceunitQuota> lambda = new QueryWrapper<BusInvoiceunitQuota>().lambda();
        lambda.eq(BusInvoiceunitQuota::getInvoiceUnitId, invoiceUnitId).eq(quotaYear > 0, BusInvoiceunitQuota::getQuotaYear, quotaYear);
        lambda.eq(StrUtil.isNotEmpty(quotaMonth), BusInvoiceunitQuota::getQuotaMonth, quotaMonth);
        return this.list(lambda);
    }

    @Override
    public void reduceMonthQuota(Map params) {
        LambdaQueryWrapper<BusInvoiceunitQuota> lambda = new QueryWrapper<BusInvoiceunitQuota>().lambda();
        lambda.eq(BusInvoiceunitQuota::getInvoiceUnitId, MapUtil.getLong(params, "invoiceUnitId")).eq(BusInvoiceunitQuota::getQuotaYear, DateUtil.thisYear()).eq(BusInvoiceunitQuota::getQuotaMonth, DateUtil.thisMonth());
        BusInvoiceunitQuota one = this.getOne(lambda);
        BigDecimal quota = new BigDecimal(MapUtil.getDouble(params, "quota"));
        one.setUseQuota(one.getUseQuota().add(quota));
        one.setResidueQuota(one.getResidueQuota().subtract(quota));
        one.updateById();
    }
}
